#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
int fla = 0;
static int callback(void *data, int argc, char **argv, char **azColName)
{
    int i;
    printf("data::%s\n", (char *)data);
    if (fla == 0)
    {
        for (i = 0; i < argc; i++)
        {
            printf("%-5s\t", azColName[i]);
            fla = 1;
        }
        printf("\n");
    }
    for (i = 0; i < argc; i++)
    {
        printf("%-5s\t", argv[i] ? argv[i] : "NULL");
    }

    printf("\n");
    return 0;
}

int show_alll()
{
    sqlite3 *db;
    int rc;
    char *sql, **dbResult, *errMsg, *buff_p, *buff_p_1;
    char buff[100] = {0};
    int nRow = 0, nColumn = 0;
    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }

    sprintf(buff, "select *  from  user ");
    sql = buff;
    sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (nRow == 0)
    {
        // sprintf(msg_masse_main, "查询错误数据库报错\n");
        sqlite3_close(db);
        return -1;
    }
    memset(buff, 0, sizeof(buff));
    char msg_temp[100] = {0};
    for (int i = 1; i <= nRow; i++)
    {
        // 各行的具体每个数据
        for (int j = 0; j < nColumn; j++)
        {
            memset(msg_temp, 0, 100);
            printf("%-5s\t", dbResult[i * nColumn + j]);
            sprintf(msg_temp, "%-5s\t", dbResult[i * nColumn + j]);
            // strcat(msg_masse_main, msg_temp);
        }
        // sprintf(msg_temp, "msg_masse_main 的长度%ld\t", strlen(msg_masse_main));
        // strcat(msg_masse_main, msg_temp);
        // strcat(msg_masse_main, "\n");
        printf("msg_temp %s \n", msg_temp);
    }

    // sprintf(msg_masse_main, "--%s", buff_p);
    return 0;
}

int main(int argc, char *argv[])
{
    show_alll();
    // sqlite3 *db;
    // char *zErrMsg = 0;
    // int rc;
    // char *sql;
    // char *data = "Callback function called";

    // /* Open database */
    // rc = sqlite3_open("test.db", &db);
    // if (rc)
    // {
    //     fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    //     exit(0);
    // }
    // else
    // {
    //     fprintf(stderr, "Opened database successfully\n");
    // }

    // /* Create SQL statement */
    // // sql = "SELECT * from COMPANY";
    // sql = "SELECT * from user where ID =1";

    // /* Execute SQL statement */
    // //
    // char buff_1[100] = {0};
    // rc = sqlite3_exec(db, sql, callback, data, &zErrMsg);
    // printf("rc::%d  sql:%s\n", rc, sql);
    // if (rc != SQLITE_OK)
    // {
    //     fprintf(stderr, "SQL error: %s\n", zErrMsg);
    //     sqlite3_free(zErrMsg);
    // }
    // else
    // {
    //     fprintf(stdout, "Operation done successfully\n");
    // }
    // sqlite3_close(db);
    // return 0;
}